package testdb;

import java.io.File;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class RecordProducer {

    PrintWriter writer;

    public RecordProducer(PrintWriter writer) {
        this.writer = writer;
    }

    public Record getFilledRecord() {
        Record record = new Record();

        try {
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            f.setValidating(false);
            DocumentBuilder builder = f.newDocumentBuilder();
            Document doc = builder.parse(new File("..\\data\\record_data.xml"));
            Element root = doc.getDocumentElement();
            Node node;
            
            node = root.getElementsByTagName("count").item(0);
            record.setCount(Integer.parseInt(node.getTextContent()));
            
            node = root.getElementsByTagName("column_1").item(0);
            record.setCol1(node.getTextContent());
            
            node = root.getElementsByTagName("column_2").item(0);
            record.setCol2(node.getTextContent());
            
            node = root.getElementsByTagName("column_3").item(0);
            record.setCol3(node.getTextContent());
            
            node = root.getElementsByTagName("column_4").item(0);
            record.setCol4(node.getTextContent());
            
            node = root.getElementsByTagName("column_5").item(0);
            record.setCol5(node.getTextContent());
            
            //writer.format("Name: %s", node.getTextContent());
            
        } catch (Exception ex) {
            ex.printStackTrace(writer);
        }

        return record;
    }
}
